package com.zc.client.http;


import cn.hutool.core.util.ByteUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.zc.client.http.enums.ServiceType;
import com.zc.client.http.enums.SourceType;
import com.zc.client.http.form.LoginFrom;
import com.zc.client.http.form.SpaceshipForm;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.json.JsonTest;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;

/**
 * 游戏相关客户端
 * 游戏下坑位，需要反复定义参数
 * 坑位比较多的情况下，需要手动点击的次数太多，难以看到效果
 * 所以有了想法，通过程序，遍历填写参数发送请求
 */
public class GameClient {
    public String queryParam = "t=B&s=a536964b9d984ae5821832db7b54a971&t=I&v=1.0.1.1&d=XX iPhone&m=Mi 6&n=wifi&r=cmcc&o=ios10.1&i=fe123ab212cd1321bcam1&h=13798121516&c=WDGG&e=123&L=zh&";

    @Test
    public void happyFarm() throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
        HttpUrl.Builder urlBuilder = HttpUrl.get("http://localhost:7777/user/farm/info").newBuilder();
        String queryParam = "t=I&s=b9511c72d18a41bb943db6fc1e86f0ec&t=I&v=1.0.1.1&d=XX iPhone&m=Mi 6&n=wifi&r=cmcc&o=ios10.1&i=fe123ab212cd1321bcam1&h=13798121516&c=WDGG&e=123&L=zh&";
        for (String part : queryParam.split("&")) {
            String[] kv = part.split("=", 2); // 分割 key=value，最多分割 2 次
            if (kv.length == 2) {
                urlBuilder.addQueryParameter(kv[0], kv[1]);
            }
        }
        HttpUrl httpUrl = urlBuilder.build();
        Request.Builder builder = new Request.Builder();
        byte[] requestBody = new byte[10];
        Request.Builder url = builder.post(RequestBody.create(requestBody)).url(httpUrl);
        url.header("X-INF", queryParam);
        Request request = new Request(url);
        Response execute = okHttpClient.newCall(request).execute();
        byte[] bytes = execute.body().byteStream().readAllBytes();
        Charset charset = execute.body().contentType().charset(StandardCharsets.UTF_8);
        String s = new String(bytes, charset);
        System.out.println(s);
    }

    public final String TEST = "http://testapi.langlang88.com";
    public final String DEV = "http://localhost:7777";

    @Test
    public void testHttpUtils() {
        HttpRequest post = HttpUtil.createPost(TEST + "/user/farm/baiting");
        String queryParam = "t=B&s=ff9a4f1b2b084e4c8220da27bf886dcb&t=I&v=1.0.1.1&d=XX iPhone&m=Mi 6&n=wifi&r=cmcc&o=ios10.1&i=fe123ab212cd1321bcam1&h=13798121516&c=WDGG&e=123&L=zh&";
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("X-INF", queryParam);
        HttpRequest httpRequest = post.addHeaders(objectObjectHashMap);
//        String requestBody = JSONUtil.toJsonStr()
//        httpRequest.body();
        for (int i = 0; i < 6; i++) {
            HashMap<String, Integer> requestBodyMap = getRequestBody(i);
            String requestBody = JSONUtil.toJsonStr(requestBodyMap);
            httpRequest.body(requestBody);
            HttpResponse execute = httpRequest.execute();
            byte[] bytes = execute.bodyBytes();
            Charset charset = Charset.forName(String.valueOf(StandardCharsets.UTF_8));
            String s = new String(bytes, charset);
            System.out.println(s);
        }
    }

    @NotNull
    private static HashMap<String, Integer> getRequestBody(Integer place) {
        HashMap<String, Integer> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("count", 100);
        objectObjectHashMap.put("place", place);
        return objectObjectHashMap;
    }

    @Test
    public void testFish() {
        HttpRequest post = HttpUtil.createPost("http://localhost:7777/user/fish/baiting");
        String queryParam = "t=B&s=b9511c72d18a41bb943db6fc1e86f0ec&t=I&v=1.0.1.1&d=XX iPhone&m=Mi 6&n=wifi&r=cmcc&o=ios10.1&i=fe123ab212cd1321bcam1&h=13798121516&c=WDGG&e=123&L=zh&";
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("X-INF", queryParam);
        HttpRequest httpRequest = post.addHeaders(objectObjectHashMap);
//        String requestBody = JSONUtil.toJsonStr()
//        httpRequest.body();
        for (int i = 0; i < 8; i++) {
            HashMap<String, Object> requestBodyMap = getFishRequestBody(i);
            String requestBody = JSONUtil.toJsonStr(requestBodyMap);
            httpRequest.body(requestBody);
            HttpResponse execute = httpRequest.execute();
            byte[] bytes = execute.bodyBytes();
            Charset charset = Charset.forName(String.valueOf(StandardCharsets.UTF_8));
            String s = new String(bytes, charset);
            System.out.println(s);
        }
    }

    @Test
    public void testLive() {
        HttpRequest post = HttpUtil.createPost("https://api.langlang88.com/live/robot/leave");
        String queryParam = "t=B&s=850689a29bfd42bb9e00947f27b6e2ff&t=I&v=1.0.1.1&d=XX iPhone&m=Mi 6&n=wifi&r=cmcc&o=ios10.1&i=fe123ab212cd1321bcam1&h=13798121516&c=WDGG&e=123&L=zh&";
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("X-INF", queryParam);
        objectObjectHashMap.put("x-forwarded-for", "115.198.161.90"); // 线上环境,需加入白名单才能访问
        HttpRequest httpRequest = post.addHeaders(objectObjectHashMap);
        HashMap<String, Object> requestBodyMap = new HashMap<>();
        requestBodyMap.put("programId", 6547);
        String requestBody = JSONUtil.toJsonStr(requestBodyMap);
        httpRequest.body(requestBody);
        HttpResponse execute = httpRequest.execute();
        byte[] bytes = execute.bodyBytes();
        Charset charset = Charset.forName(String.valueOf(StandardCharsets.UTF_8));
        String s = new String(bytes, charset);
        System.out.println(s);
    }

    public void sendEggOrBox(String url, String sid, int giftId, int count, int pid) {
        HttpRequest post = HttpUtil.createPost(url);
        String queryParam = this.queryParam;
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("X-INF", queryParam);
        HttpRequest httpRequest = post.addHeaders(objectObjectHashMap);
        HashMap<String, Object> requestBodyMap = new HashMap<>();
        requestBodyMap.put("programId", pid);
        requestBodyMap.put("giftId", giftId);
        requestBodyMap.put("count", count);
        String requestBody = JSONUtil.toJsonStr(requestBodyMap);
        httpRequest.body(requestBody);
        HttpResponse execute = httpRequest.execute();
        byte[] bytes = execute.bodyBytes();
        Charset charset = Charset.forName(String.valueOf(StandardCharsets.UTF_8));
        String s = new String(bytes, charset);
        System.out.println(s);
    }

    @Test
    public void testDrawUniversePatch() {
        HttpRequest post = HttpUtil.createPost("http://localhost:8888/live/program/more/universePatchAward");
        String queryParam = this.queryParam;
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("X-INF", queryParam);
        HttpRequest httpRequest = post.addHeaders(objectObjectHashMap);
        HashMap<String, Object> requestBodyMap = new HashMap<>();
        requestBodyMap.put("money", 100.1);
        requestBodyMap.put("programId", 2519);
        requestBodyMap.put("anchorId", 0);
        String requestBody = JSONUtil.toJsonStr(requestBodyMap);
        httpRequest.body(requestBody);
        HttpResponse execute = httpRequest.execute();
        byte[] bytes = execute.bodyBytes();
        Charset charset = Charset.forName(String.valueOf(StandardCharsets.UTF_8));
        String s = new String(bytes, charset);
        System.out.println(s);
    }

    @Test
    public void testUniversePatch() {
        HttpRequest post = HttpUtil.createPost("http://localhost:8888/live/program/more/universePatch");
        String queryParam = this.queryParam;
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("X-INF", queryParam);
        HttpRequest httpRequest = post.addHeaders(objectObjectHashMap);
        HashMap<String, Object> requestBodyMap = new HashMap<>();
        requestBodyMap.put("limit", 10);
        requestBodyMap.put("offset", null);
        requestBodyMap.put("programId", 2519);
        String requestBody = JSONUtil.toJsonStr(requestBodyMap);
        httpRequest.body(requestBody);
        HttpResponse execute = httpRequest.execute();
        byte[] bytes = execute.bodyBytes();
        Charset charset = Charset.forName(String.valueOf(StandardCharsets.UTF_8));
        String s = new String(bytes, charset);
        System.out.println(s);
    }

    @Test
    public void testHitEgg() {
        HttpRequest postBox = HttpUtil.createPost("http://localhost:7777/live/room/consume/sendGift");
        // 盲盒 {"count":1,"giftId":487,"programId":36460}
        // 红龙蛋 {"count":1,"giftId":-2482,"programId":36460}
        // 黑龙蛋 {"count":1,"giftId":-2483,"programId":36460}
        // 霸王龙仔 {"count":1,"giftId":-2220,"programId":36460}
        HttpRequest postHitEgg_Normal = HttpUtil.createPost("http://localhost:7777/live/room/consume/sendGift");
        int pid = 32777;
        sendEggOrBox("http://localhost:7777/live/room/consume/sendGift", "", -2001, 1, pid);
        sendEggOrBox("http://localhost:7777/live/room/consume/sendGift", "", -2001, 10, pid);
    }

    @NotNull
    private static HashMap<String, Object> getFishRequestBody(Integer place) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("count", 100);
        objectObjectHashMap.put("newVersion", false);
        objectObjectHashMap.put("place", place);
        return objectObjectHashMap;
    }

    /**
     * 测试登录
     */
    @Test
    public void testLogin() {
        SendGiftClient sendGiftClient = new SendGiftClient(SourceType.DEV, ServiceType.LUKE_DEV, "/user/spaceship/info");
        sendGiftClient.creatPost();
        String session = sendGiftClient.fillSession("13858749873");
        System.out.println(session);
        sendGiftClient.postExecute(new LoginFrom());
    }

    /**
     * 柠檬测试登录
     */
    @Test
    public void testLeLeLogin() {
        SendGiftClient sendGiftClient = new SendGiftClient(SourceType.DEV, ServiceType.LELE_DEV, "/user/spaceship/info");
        sendGiftClient.creatPost();
        String session = sendGiftClient.fillSession("13858749873");
//        String session = sendGiftClient.fillSession("18266664444");
        System.out.println(session);
        sendGiftClient.postExecute(new LoginFrom());
    }

    /**
     * 星际战舰 -下饵
     */
    /*3dbcfe58d0d24d89b2295086ad71c8f6*/
    @Test
    public void testSpaceshipBaits() {
        // 查看info
        SendGiftClient sendGiftClient = new SendGiftClient(SourceType.DEV, ServiceType.LUKE_DEV, "/user/spaceship/info");
        sendGiftClient.creatPost();
        sendGiftClient.setSessionId("3dbcfe58d0d24d89b2295086ad71c8f6");
        System.out.println("----------下饵前----------");
        sendGiftClient.postExecute(null);
        // 下饵 每坑位下10饵
        sendGiftClient.setPort("/user/spaceship/baiting");
        sendGiftClient.creatPost();
        for (int place = 0; place < 1; ) {
            SpaceshipForm spaceshipForm = new SpaceshipForm();
            spaceshipForm.setCount(10L);
            spaceshipForm.setProgramId(2519);
            spaceshipForm.setPlace(place);
            sendGiftClient.postExecute(spaceshipForm);
            place++;
        }
        // 查看info
        System.out.println("----------下饵后----------");
        sendGiftClient.setPort("/user/spaceship/info");
        sendGiftClient.creatPost();
        sendGiftClient.postExecute(null);
    }

    /**
     * 星际战舰 - 查看日志
     */
    @Test
    public void testSpaceshipLogs() {
        // 查看我的日志
        System.out.println("----------我的日志----------");
        SendGiftClient sendGiftClient = new SendGiftClient(SourceType.DEV, ServiceType.LUKE_DEV, "/user/spaceship/myLogs");
        sendGiftClient.creatPost();
        sendGiftClient.setSessionId("3dbcfe58d0d24d89b2295086ad71c8f6");
        sendGiftClient.postExecute(null);
        // 查看30场日志
        System.out.println("----------30场日志----------");
        sendGiftClient.setPort("/user/spaceship/lastLogs");
        sendGiftClient.creatPost();
        sendGiftClient.postExecute(null);
        // 查看100场日志
        System.out.println("----------100场日志----------");
        sendGiftClient.setPort("/user/spaceship/totalLogs");
        sendGiftClient.creatPost();
        sendGiftClient.postExecute(null);
    }

    /**
     *  星际战舰 - 查看info
     */
    @Test
    public void testSpaceshipInfo() {
        // 查看info
        SendGiftClient sendGiftClient = new SendGiftClient(SourceType.DEV, ServiceType.LUKE_DEV, "/user/spaceship/info");
        sendGiftClient.creatPost();
        sendGiftClient.setSessionId("3dbcfe58d0d24d89b2295086ad71c8f6");
        sendGiftClient.postExecute(null);
    }

    /**
     *  星际战舰 - CMS游戏看板
     */
    @Test
    public void testSpaceshipGameData() {
        SendGiftClient sendGiftClient = new SendGiftClient(SourceType.DEV, ServiceType.LUKE_CMS, "/dataStatistic/spaceship");
        sendGiftClient.creatPost();
        sendGiftClient.setSessionId("3dbcfe58d0d24d89b2295086ad71c8f6");
        sendGiftClient.postExecute(null);
    }
}
